package org.jingle.sketch.component
{
	import org.jingle.sketch.GraphicsUtils;
	
	public class Arc extends Composite
	{
		private var _radius:Number;
		
		private var _startAngle:Number = 0;
		private var _start:Point;
		private var _end:Point;
		
		public function Arc(parent:Composite, origin:Point, radius:Number, angle:Number)
		{
			super(parent);
			this.origin = origin;
			this.radius = radius;
			this.angle = angle;
		}
		
		public function set radius(r:Number):void {
			this._radius = r;
		}
		
		public function get radius(): Number {
			return this._radius;
		}
		
		public override function init(): void {
			sprite.graphics.clear();
			sprite.x = origin.x;
			sprite.y = origin.y;
			
			sprite.graphics.lineStyle(thickness, lineColor, alpha);
			if(fillColor != uint.MAX_VALUE){
				sprite.graphics.beginFill(fillColor);
			}
			if (Math.abs(angle) > 360)
			{
				angle=360;
			}
			var n:Number = Math.ceil(Math.abs(angle) / 45);
			var angleA:Number=angle / n;
			var startA:Number = _startAngle;
			angleA = angleA * Math.PI / 180;
			startA = startA * Math.PI / 180;
			var startB:Number = startA;
			var R:Number = radius;
			var x:int = 0;
			var y:int = 0;
			
			this._start = new Point(x + R * Math.cos(startA), y + R * Math.sin(startA));
			sprite.graphics.moveTo(_start.x, _start.y);
			
			for (var i:uint=1; i <= n; i++) {
				startA += angleA;
				var angleMid1:Number=startA - angleA / 2;
				var bx:Number = x + R / Math.cos(angleA / 2) * Math.cos(angleMid1);
				var by:Number = y + R / Math.cos(angleA / 2) * Math.sin(angleMid1);
				var cx:Number = x + R * Math.cos(startA);
				var cy:Number = y + R * Math.sin(startA);
				sprite.graphics.curveTo(bx, by, cx, cy);
				if(i == n){
					this._end = new Point(cx, cy);
				}
			}
			if(fillColor != uint.MAX_VALUE){
				sprite.graphics.endFill();
			}
		 	super.init();
		}
		
		public function  set startAngle(a:Number): void {
			this._startAngle = a;
		}
		
		public function get startAngle(): Number {
			return this._startAngle;
		}
		
		internal function getStart(): Point{
			return this._start;
		} 
		
		internal function getEnd(): Point {
			return this._end;
		}
	}
}